﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Taurus.Parking.DTO;
using Taurus.Platform.XML;
using System.Xml.Linq;

namespace Taurus.Parking.XML
{
    class CarPersistence : BasePersistence<Car> 
    {
        public override string FileName
        {
            get { return "AutomobileMarket.xml"; }
        }

        public override System.Xml.Linq.XElement GetEl(Car item)
        {
            var t = (from el in Root.Elements("Car") where el.Attribute("ID").Value == item.ID.ToString() select el).FirstOrDefault();
            return t;
        }

        public override Car GetObject(string id)
        {
            XElement el = GetEl(new Car() { ID = int.Parse(id) });
            return XChange(el);
        }

        protected override System.Xml.Linq.XElement XChange(Car item)
        {
            XElement car = new XElement("Car");
            car.SetAttributeValue("ID", item.ID);
            car.SetAttributeValue("Name", item.Name);
            car.SetAttributeValue("Pic", item.PicFullPath);
            car.SetAttributeValue("Price", item.Price);
            return car;

        }

        protected override Car XChange(System.Xml.Linq.XElement item)
        {
            Car car = new Car();
            car.Price = item.Attribute("Price").Value;
            car.PicFullPath = item.Attribute("Pic").Value;
            car.Name = item.Attribute("Name").Value;
            car.ID = int.Parse(item.Attribute("ID").Value);
            return car;
           
        }
    }
}
